package org.meb.conquest.rest.json.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.meb.conquest.db.model.Deck;
import org.meb.conquest.db.model.DeckMember;
import org.meb.conquest.db.model.loc.Card;
import org.meb.conquest.db.util.Utils;

@Getter
@Setter
@NoArgsConstructor
public class JsonDeck {

	private Long id;
	private String techName;
	private Long warlordId;
	private String name;
	private String description;
	private Date createDate;
	private Date modifyDate;
	private Integer configCsQuantity; 
	private List<JsonDeckMember> members = new ArrayList<>();

	public JsonDeck(Deck deck) {
		this(deck, false);
	}

	public JsonDeck(Deck deck, boolean includeMembers) {
		id = deck.getId();		
		warlordId = deck.getWarlord().getId();
		name = deck.getName();
		if (name != null) {
			techName = Utils.toTechName(name);
		}
		description = deck.getDescription();
		createDate = deck.getCreateDate();
		modifyDate = deck.getModifyDate();
		configCsQuantity = deck.getConfigCsQuantity();
		
		if (includeMembers) {
			for (DeckMember deckMember : deck.getDeckMembers()) {
				members.add(new JsonDeckMember(deckMember));
			}
		}
	}
	
	public Deck toDeck() {
		Deck deck = new Deck();
		deck.setId(getId());
		deck.setName(getName());
		deck.setDescription(getDescription());
		deck.setCreateDate(getCreateDate());
		deck.setModifyDate(getModifyDate());
		deck.setConfigCsQuantity(getConfigCsQuantity());
		deck.setWarlord(new Card(getWarlordId()));
		Set<DeckMember> members = new LinkedHashSet<>();
		for (JsonDeckMember jdm : getMembers()) {
			DeckMember member = new DeckMember();
			member.setCard(new Card(jdm.getCardId()));
			member.setQuantity(jdm.getQuantity());
			members.add(member);
		}
		deck.setDeckMembers(members);
		return deck;
	}
}
